package org.gwt.grin.rebind.features;

import org.gwt.grin.client.features.Assembly;
import org.gwt.grin.rebind.SEFeature;
import org.gwt.grin.rebind.ShowBuilder;

import com.google.gwt.uibinder.rebind.IndentedWriter;

public class SEAssembly extends SEFeature {

	private String[] partNames;
	private SEFeature[] parts;

	public SEAssembly(String name) {
		super(name);
	}

	protected String getClassName() {
		return Assembly.class.getName();
	}

	public void setup(String[] partNames, SEFeature[] parts) {
		this.partNames = partNames;
		this.parts = parts;
	}

	@Override
	public void setup(IndentedWriter w, ShowBuilder builder) {
		String vn = getVN(builder);
		w.write("%s.setup(%s, %s);", vn, makeStringArray(partNames),
				makeFeaturesArray(builder, parts));
	}

	/**
	 * Find the part of the given name in this assembly, or {@code null} if it
	 * can't be found.
	 * 
	 * @param name
	 *            the part name to search for
	 * @return the part of the given name
	 */
	public SEFeature findPart(String name) {
		for (int i = 0; i < parts.length; i++) {
			if (partNames[i].equals(name)) {
				return parts[i];
			}
		}
		return null;
	}
}
